import { Service } from 'egg';
import ResponseListModel from './../model/responseListModel';
import {
  IQueryListCommonParams,
  ESysErrorCode,
} from '../abstract/interface/common';

export interface IQueryMonitorErrorListParams {
  errorType: string;
  errorLevel: string;
  appSystem: string;
  environment: string;
  startTime: string;
  endTime: string;
}

export default class MonitorErrorService extends Service {
  /**
   * 查询错误信息列表
   */
  public async queryMonitorErrorList(
    params: Partial<IQueryMonitorErrorListParams>,
    pageOptions: IQueryListCommonParams<number>,
  ) {
    const responseListModel = new ResponseListModel();
    const { page, pageSize } = pageOptions;
    try {
          const queryResult = await this.ctx.model.MonitorError.find(
        {
          $or: [
            {
              errorType: { $regex: new RegExp(params.errorType || '') },
            },
            {
              errorLevel: { $regex: new RegExp(params.errorLevel || '') },
            },
            {
              appSystem: { $regex: new RegExp(params.appSystem || '') },
            },
            {
              environment: { $regex: new RegExp(params.environment || '') },
            },
          ],
        },
        {},
        {
          sort: {
            time: 'desc',
          },
          limit: pageSize,
          skip: (page - 1) * pageSize,
        },
      );
          console.log('params', params);
          // const queryResult = await this.ctx.model.MonitorError.find();
          const monitorErrorTotalNumber = await this.ctx.model.MonitorError.countDocuments(
            {},
          );
          responseListModel.code = ESysErrorCode.SUCCESS;
          responseListModel.message = '查询成功';
          responseListModel.success = true;
          responseListModel.data = {
            page,
            pageSize,
            list: queryResult || [],
            total: monitorErrorTotalNumber,
          };
        } catch (error) {
      this.logger.error(error);
      responseListModel.code = ESysErrorCode.CRASH;
      responseListModel.message = '系统异常';
      responseListModel.success = false;
    }
    return responseListModel;
  }

  /**
   * 查询日志信息列表
   */
  public async queryMonitorLogList(
    params: Partial<IQueryMonitorErrorListParams>,
    pageOptions: IQueryListCommonParams<number>,
  ) {
    const responseListModel = new ResponseListModel();
    const { page, pageSize } = pageOptions;
    try {
      /* const queryResult = await this.ctx.model.MonitorLog.find(
        {
          $or: [
            {
              errorType: { $regex: new RegExp(params.errorType || '') },
            },
            {
              errorLevel: { $regex: new RegExp(params.errorLevel || '') },
            },
            {
              appSystem: { $regex: new RegExp(params.appSystem || '') },
            },
            {
              environment: { $regex: new RegExp(params.environment || '') },
            },
          ],
        },
        {},
        {
          sort: {
            errorTimeStamp: 'desc',
          },
          limit: pageSize,
          skip: (page - 1) * pageSize,
        },
      ); */
      console.log('params', params);
      const queryResult = await this.ctx.model.MonitorLog.find();
      console.log('queryResult', queryResult);
      const monitorErrorTotalNumber = await this.ctx.model.MonitorLog.countDocuments(
        {},
      );
      responseListModel.code = ESysErrorCode.SUCCESS;
      responseListModel.message = '查询成功';
      responseListModel.success = true;
      responseListModel.data = {
        page,
        pageSize,
        list: queryResult || [],
        total: monitorErrorTotalNumber,
      };
    } catch (error) {
      this.logger.error(error);
      responseListModel.code = ESysErrorCode.CRASH;
      responseListModel.message = '系统异常';
      responseListModel.success = false;
    }
    return responseListModel;
  }
}
